package com.bytebuddytest.fieldandmethod;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.implementation.DefaultMethodCall;
import org.junit.Assert;
import org.junit.Test;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2023/10/20 17:20
 */
@SuppressWarnings({"resource", "unused"})
public class DefaultMethodCallTest {
    public interface First {
        default String qux() {
            return "FOO";
        }
    }

    public interface Second {
        default String qux() {
            return "BAR";
        }
    }

    @Test
    public void testPrioritize() throws Exception {
        Class<?> dynamicClass = new ByteBuddy(ClassFileVersion.JAVA_V8)
                .subclass(Object.class)
                .implement(First.class)
                .implement(Second.class)
                .method(named("qux")).intercept(DefaultMethodCall.prioritize(First.class))
                .make()
                .load(ClassLoader.getSystemClassLoader())
                .getLoaded();
        Object ret = dynamicClass.getDeclaredMethod("qux")
                .invoke(dynamicClass.newInstance());
        Assert.assertEquals("FOO", ret);
    }

}
